﻿using Huayou.Common.Database;
using Huayou.Utils;
using HySoft.Cash.BaseClass;
using HySoft.Cash.Structure;
using System;
using System.Data;
using Huayou.Common;

namespace HySoft.Cash.DB
{
    public class ReportSettingsDB : BaseDB
    {
        public ReportSettingsDB(DBOperate oper)
            : base(oper)
        {

        }

        public bool OperateReportSettings<T>(T detail) where T : ReportSettingsDT
        {
            int num = 0;
            string settingKey = detail.ReportSettingName;
            SerializerData data = detail.EncodeSerializerData(settingKey);

            string sqlSelect = string.Format("SELECT COUNT(*) FROM ReportSettings WHERE UserID={0}", detail.UserID);
            string sqlInsert = string.Format("INSERT INTO ReportSettings (UserID,{0}Keys,{0}Values) VALUES ({1},'{2}','{3}')", settingKey, detail.UserID, data.Keys, data.Values);
            string sqlUpdate = string.Format("UPDATE ReportSettings SET {0}Keys='{2}',{0}Values='{3}' WHERE UserID={1}", settingKey, detail.UserID, data.Keys, data.Values);

            try
            {
                try
                {
                    num = Convert.ToInt32(base.ExecuteForScalar(sqlSelect));
                    if (num > 0)
                    {
                        num = base.ExecuteForInt(sqlUpdate);
                    }
                    else
                    {
                        num = base.ExecuteForInt(sqlInsert);
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public T GetReportSettings<T>(int userID) where T : ReportSettingsDT, new()
        {
            T detail = new T();

            try
            {
                string settingKey = detail.ReportSettingName;
                string strSql = string.Format("SELECT UserID,{1}Keys,{1}Values FROM ReportSettings WHERE UserID={0}", userID, settingKey); //"select * from tBankInfo where (Sid=" + Sid + ")";
                DataTable table = base.ExecuteForDataTable(strSql);
                DataRow row = null;
                if (table.Rows.Count > 0)
                {
                    row = table.Rows[0];
                    detail.UserID = Convert.ToInt32(row["UserID"]);
                    SerializerData data = new SerializerData();
                    string keyColumnName= string.Format("{0}Keys",settingKey);
                    string valueColumnName= string.Format("{0}Values",settingKey);
                    data.Keys = Convert.ToString(row[keyColumnName]);
                    data.Values = Convert.ToString(row[valueColumnName]);
                    detail.ParseSerializerData(settingKey, data);
                }
            }
            finally
            {
            }
            return detail;
        }
    }
}
